//将班级详情表单清空的方法
function classesDetailFormClean() {
    classesDetailForm.reset();
    classesDetailTabPanel.classesId = null;
    classesDetailTabPanel.isCreate = null;
    classesDetailForm.down('#creater').createrId = null;
    classesProcessForm.reset();
    classesProcessForm.processId = null;
    classesProcessForm.down('#toDirector').toDirector = null;
    classesMemberGridStore.removeAll();
}

//获取班级详细信息
function getClassesDetail(id) {
    var result;
    Ext.Ajax.request({
        url: zuulUrl + '/' + authServiceName + '/classes/forAdmin/detail/' + id,
        method: 'get',
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            obj.createDate = obj.createDate.replace('T', '  ');
            result = obj;
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return result;
}

//将数据填充到表单
function putDataIntoForm(data) {
    classesDetailTabPanel.classesId = data.id;
    var enterTimeFieldSet = classesProcessForm.down('#enterTime');
    var memberCountFieldSet = classesProcessForm.down('#memberCount');
    classesDetailForm.down('#name').setValue(data.name);
    classesDetailForm.down('#creater').createrId = data.creater;
    classesDetailForm.down('#creater').setValue(data.createrName);
    classesDetailForm.down('#stage').setValue(data.stage);
    classesDetailForm.down('#createDate').setValue(data.createDate);
    classesDetailForm.down('#describe').setValue(data.describe);
    classesMemberGridStore.setData(data.members);
    classesProcessForm.processId = data.process.id;
    var enterBeginIndex = data.process.enterBeginTime.indexOf('T');
    var enterEndIndex = data.process.enterEndTime.indexOf('T');
    enterTimeFieldSet.down('#beginDate').setValue(data.process.enterBeginTime.substring(0, enterBeginIndex));
    enterTimeFieldSet.down('#beginTime').setValue(data.process.enterBeginTime.substring(enterBeginIndex + 1, enterBeginIndex + 6));
    enterTimeFieldSet.down('#endDate').setValue(data.process.enterEndTime.substring(0, enterEndIndex));
    enterTimeFieldSet.down('#endTime').setValue(data.process.enterEndTime.substring(enterEndIndex + 1, enterEndIndex + 6));
    memberCountFieldSet.down('#minMember').setValue(data.process.minMemberCount);
    memberCountFieldSet.down('#maxMember').setValue(data.process.maxMemberCount);
    classesProcessForm.down('#toDirector').setValue(data.process.toDirectorName);
    classesProcessForm.down('#toDirector').toDirector = data.process.toDirector;
}

//点击查看详情之后要做的事情
function seeDetailClick(record) {
    Ext.getCmp('centerRegion').removeAll(false);
    Ext.getCmp('centerRegion').add(classesDetailTabPanel);
    classesDetailFormClean();
    classesDetailTabPanel.setActiveTab(0);
    classesDetailTabPanel.isCreate = 0;
    var data = getClassesDetail(record.get('id'));
    putDataIntoForm(data);
}

//点击手动完成开班之后要做的事情
function manualOpenClassesClick(record) {
    Ext.Ajax.request({
        url: zuulUrl + '/' + authServiceName + '/classes/forAdmin/manual/Process',
        method: 'put',
        async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        params: {
            classesId: record.get('id'),
            currentUserId: currentUserId
        },
        success: function (response, opts) {
            classesListStore.remove(record);
            Ext.Msg.alert("成功", "手动开班成功");
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 401 || obj.code == 402 || obj.code == 403 || obj.code == 406 || obj.code == 407)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
}

//保存班级详情
function saveClassesDetail(isManual) {
    var toDirector = classesProcessForm.down('#toDirector').toDirector;
    if (classesDetailForm.isValid() && toDirector != null && (isManual || classesProcessForm.isValid())) {
        var result = new Object();
        result['id'] = classesDetailTabPanel.classesId;
        result['name'] = classesDetailForm.down('#name').getValue();
        result['describe'] = classesDetailForm.down('#describe').getValue();
        result['creater'] = classesDetailForm.down('#creater').createrId;
        var memberArray = new Array();
        Ext.each(classesMemberGridStore.getData().items, function (member) {
            memberArray.push(member.data.id);
        });
        result['classesMember'] = memberArray;
        result['currentUserId'] = currentUserId;
        var enterTimeFieldSet = classesProcessForm.down('#enterTime');
        var memberCountFieldSet = classesProcessForm.down('#memberCount');
        var beginDate = enterTimeFieldSet.down('#beginDate').getValue();
        var beginTime = enterTimeFieldSet.down('#beginTime').getValue();
        var endDate = enterTimeFieldSet.down('#endDate').getValue();
        var endTime = enterTimeFieldSet.down('#endTime').getValue();
        var beginDateTime = getFormatDateDateTime(beginDate, beginTime);
        var endDateTime = getFormatDateDateTime(endDate, endTime);
        var process = new Object();
        process['toDirector'] = toDirector;
        result['isManual'] = isManual;
        if (!isManual) {//如果不是手动的话才要储存流程细节
            var minMemberCount = memberCountFieldSet.down("#minMember").getValue();
            var maxMemberCount = memberCountFieldSet.down("#maxMember").getValue();
            if (beginDate == null || beginTime == null ||endDate == null || endTime == null || minMemberCount == null || maxMemberCount == null) {
                Ext.Msg.alert('错误', '流程信息请填写完全！');
                return;
            }
            if (!checkTimeIsOrder(beginDateTime, endDateTime)) {
                Ext.Msg.alert('错误', '流程开始时间必须早于结束时间！');
                return;
            }
            process['id'] = classesProcessForm.processId;
            process['toClasses'] = classesDetailTabPanel.classesId;
            process['enterBeginTime'] = beginDateTime;
            process['enterEndTime'] = endDateTime;
            process['minMemberCount'] = minMemberCount;
            process['maxMemberCount'] = maxMemberCount;
            result['process'] = process;
            if (memberArray.length >= maxMemberCount) {
                Ext.MessageBox.confirm('警告', '该班级人数已到达规划人数上限,是否直接开班？', function (btn) {
                    if (btn === 'yes') {
                        if (saveClassesAjax(result)) {
                            backButtonClick();
                            Ext.Msg.alert("成功", "保存成功");
                        }
                    }
                });
            }else{
                if (saveClassesAjax(result)) {
                    backButtonClick();
                    Ext.Msg.alert("成功", "保存成功");
                }
            }
        }else{
        	result['process'] = process;
            if (saveClassesAjax(result)) {
                classesDetailTabPanel.isCreate = 0;
                if (isManual)//为了防止他手动完成还修改
                    backButtonClick();
                Ext.Msg.alert("成功", "保存成功");
            }
        }
    } else
        Ext.Msg.alert('错误', '表单信息请填写完全！');
}

//请求提交班级
function saveClassesAjax(result) {
	var flag=false;
    Ext.Ajax.request({
        url: zuulUrl + '/' + authServiceName + '/classes/forAdmin',
        method: classesDetailTabPanel.isCreate == 1 ? 'post' : 'put',
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        defaultXdrContentType: 'application/json',
        jsonData: JSON.stringify(result),
        success: function (response, opts) {
        	flag=true;
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 401 || obj.code == 402 || obj.code == 403 || obj.code == 406 || obj.code == 407)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return flag;
}

//点击返回按钮之后的操作
function backButtonClick() {
    Ext.getCmp('centerRegion').removeAll(false);
    classesListStore.reload();
    Ext.getCmp('centerRegion').add(classesGrid);
}

//点击添加班级之后的操作
function addClassesButtonClick() {
    Ext.getCmp('centerRegion').removeAll(false);
    Ext.getCmp('centerRegion').add(classesDetailTabPanel);
    classesDetailTabPanel.setActiveTab(0);
    classesDetailFormClean();
    classesDetailTabPanel.isCreate = 1;
    classesDetailTabPanel.classesId = getUuid();
    classesProcessForm.processId = getUuid();
    classesDetailForm.down('#creater').createrId = currentUserId;
    classesDetailForm.down('#creater').setValue(currentUser);
}

//点击删除班级之后的操作
function deleteClassesButtonClick() {
    var selectArray = classesGridCheckBox.getSelection();
    if (selectArray.length == 0)
        Ext.Msg.alert("错误", "请选择要删除的班级！");
    else {
        var result = new Array();
        Ext.each(selectArray, function (item) {
            result.push(item.data.id);
        });
        Ext.Ajax.request({
            url: zuulUrl + '/' + authServiceName + '/classes/forAdmin/del/' + currentUserId,
            method: 'put',
            async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            defaultXdrContentType: 'application/json',
            jsonData: JSON.stringify(result),
            success: function (response, opts) {
                Ext.Msg.alert("成功", "删除成功");
                Ext.each(selectArray, function (item) {
                    classesListStore.remove(item);
                });
            },
            failure: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                if (obj.code == 401 || obj.code == 402 || obj.code == 403 || obj.code == 406 || obj.code == 407)
                    Ext.Msg.alert("错误", obj.message);
                else
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
    }
}